package com.sensorfriendly.ui;

import com.sensorfriendly.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * customized toggle button to simulate slding on/off switch.
 *
 */

public class MyToggleButton extends TextView implements OnClickListener{
	
	private boolean activated = true;
    private Context context;
	
	public MyToggleButton(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public MyToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public MyToggleButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init();
    }
	
	private void init() {
		this.toggle(activated);
		this.setOnClickListener(this);
		
	}

	public void toggle(boolean activate){
		this.activated = activate;
		if(activated == true){
			Drawable image = context.getResources().getDrawable(R.drawable.on);
			this.setCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
		}
		else{
			Drawable image = context.getResources().getDrawable(R.drawable.off);
			this.setCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
		}
	}
	
	public void toggle(){
		if(activated == false){
			activated = true;
			Drawable image = context.getResources().getDrawable(R.drawable.on);
			this.setCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
		}
		else{
			activated = false;
			Drawable image = context.getResources().getDrawable(R.drawable.off);
			this.setCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
		}
	}
	

	@Override
	public void onClick(View v) {
		if(v.getId() == this.getId()){
			if(activated == false){
				activated = true;
				Drawable image = context.getResources().getDrawable(R.drawable.on);
				this.setCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
			}
			else{
				activated = false;
				Drawable image = context.getResources().getDrawable(R.drawable.off);
				this.setCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
			}
		}
	}
	
	public boolean isActivated(){
		return activated;
	}
	
	
	

}
